﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 文件阅办删除
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class TaskdelController : Common.AdminBaseController
    {
        /// <summary>
        /// 文件阅办删除
        /// </summary>
        /// <returns>返回文件阅办删除主页面视图</returns>
        [AuthorityActionFilter("m_Taskdel", Order = 1)]
        [LogPageActionFilter("文件阅办删除", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FTaskClassCd in ( select FTaskClassCd from dbo.t_TaskAuthority where FLogID='" + OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) + "'and FAuthorityType='ADD') ";
            List<Model.Task> TaskList = GetTaskListList(strwhere, 1);
            return View(TaskList);
        }



        public ActionResult TaskdelPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            string type = Request.Params["Type"];
            string gNO = Request.Params["gNO"];
            string dutyNo = Request.Params["DutyNo"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = " FTaskClassCd in ( select FTaskClassCd from dbo.t_TaskAuthority where FLogID='" + OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) + "' and FAuthorityType='ADD')";
            List<Model.Task> TaskList = GetTaskListList(strwhere, pageIndex);


            return PartialView("TaskdelPartial", TaskList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.Task> GetTaskListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.Task TaskBLL = new BLL.Task();
            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("v_Task", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FEndDate", 1, out total);
            List<ElingOA6_Admin_DSJCJ.Model.Task> taskList = TaskBLL.DataTableToList(ds.Tables[0]);
            List<Model.Task> taskAllList = new List<Model.Task>();

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                taskAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (taskList.Count > i)
                {
                    taskAllList[((pageIndex - 1) * pageRowSize) + i] = taskList[i];
                }
            }

            return taskAllList;
        }

        /// <summary>
        /// 删除定义的请假外出事由（单个）
        /// </summary>
        /// <param name="fWorkTimeID">请假外出事由编号</param>
        /// <returns>是否成功</returns>
        public string DelTask(string FTaskID)
        {
            try
            {
                BLL.Task TaskBLL = new BLL.Task();
                TaskBLL.Delete(decimal.Parse(FTaskID));
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        /// <summary>
        /// 删除定义的请假外出事由（批量）
        /// </summary>
        /// <param name="workTimeNoArray">请假外出事由编号以逗号分隔</param>
        /// <returns>是否成功</returns>
        public string DelTaskMany(string TaskArray)
        {
            try
            {
                BLL.Task TaskBLL = new BLL.Task();
                TaskBLL.DeleteList(TaskArray);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

    }
}
